﻿using System;
using System.Drawing;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Collections;
using System.Web.Security;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Microsoft.Reporting.WebForms;
using System.Data;
using System.Data.SqlClient;

namespace Dragon
{
    public partial class wfRouteMapTracking : System.Web.UI.Page
    {
        DataService ds = new DataService();

        protected void Page_Load(object sender, EventArgs e)
        {
            //List<string> roles = new List<string> { "All", "ASM", "SS", "BAM" };
            //if (Session["Username"] == null || roles.Contains(Session["UserType"].ToString()) == false)
            //{
            //    Response.Redirect("~/wfLogin.aspx");
            //}

            if (!IsPostBack)
            {
                ds.SetData("delete from MapTracking");
            }
        }

        protected void btnTracking_Click(object sender, EventArgs e)
        {
            DataTable dt = CreateMapTracking();
            rptMarkers.DataSource = dt;
            rptMarkers.DataBind();
            ClientScript.RegisterStartupScript(this.GetType(), "alert", "InitMap();", true);
        }

        protected void btnFullStep_Click(object sender, EventArgs e)
        {
            DataTable dt = CreateMapTracking();
            rptMarkers.DataSource = dt;
            rptMarkers.DataBind();
            ClientScript.RegisterStartupScript(this.GetType(), "alert", "ViewFullStep();", true);
        }

        private DataTable CreateMapTracking()
        {
            int routingCode;
            try
            {
                routingCode = Convert.ToInt32(this.ddlRouting.SelectedValue.ToString());
            }
            catch
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "Information", "<script>alert('Input data is not correct!');</script>");
                return null;
            }

            string sql = "exec sp_Report_Route_Tracking @RoutingCode";
            SqlParameter pRoutingCode = new SqlParameter("@RoutingCode", routingCode);
            ds.SetData(sql, pRoutingCode);

            // Lay thong tin Location va Distance
            sql = "select Rt.distance, R.Location from Routing Rt inner join Region R on Rt.RegionCode = R.code where Rt.RoutingCode = " + routingCode;
            DataTable dtRouting = ds.GetData(sql);
            int distance = Convert.ToInt32(dtRouting.Rows[0][0].ToString());
            string location = dtRouting.Rows[0][1].ToString();
            this.lblInfo.Text = "Location: <b>" + location + "</b>, Distance: <b>" +  String.Format("{0:#,###,###.##}", distance) + "m</b>";

            //Hien thi du lieu len Grid
            this.GridView1.DataBind();

            DataTable dt = ds.GetData("select * from MapTracking");
            int sodong = dt.Rows.Count;
            if (sodong == 0)
                return null;
            for (int i = 0; i < sodong; i++)
                dt.Rows[i]["Description"] = dt.Rows[i]["Description"].ToString().Replace("#", "<br>");

            return dt;            
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                try
                {
                    double moved = Convert.ToDouble(e.Row.Cells[1].Text);
                    foreach (TableCell cell in e.Row.Cells)
                    {
                        if (moved >= 1000)
                        {
                            cell.BackColor = Color.Red;
                            cell.ForeColor = Color.White;
                        }
                    }
                }
                catch
                {
                    return;
                }
            }
        }
    }
}